<template>
  <div id="questionSubmitView">
    <!-- 搜索表单 -->
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="questionId" label="题号" style="min-width: 240px">
        <a-input v-model="searchParams.questionId" placeholder="请输入题号" />
      </a-form-item>
      <a-form-item field="language" label="编程语言" style="min-width: 240px">
        <a-select
          :style="{ width: '320px' }"
          placeholder="请选择编程语言"
          v-model="searchParams.language"
        >
          <a-option>java</a-option>
          <a-option>cpp</a-option>
          <a-option>go</a-option>
          <a-option>html</a-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button @click="doSubmit()" type="primary">搜索</a-button>
      </a-form-item>
    </a-form>

    <a-divider size="0"></a-divider>

    <!-- 提交记录表格 -->
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        showTotal: true,
        total,
      }"
      @page-change="onPageChange"
    >
      <!-- 判题信息 -->
      <template #judgeInfo="{ record }">
        <div v-if="record.judgeInfo">
          <a-card :bordered="false" :body-style="{ padding: '8px' }">
            <p>
              <IconInfoCircle style="color: #1890ff; margin-right: 4px" />
              <strong>信息：</strong>
              {{ parseJudgeInfo(record.judgeInfo).message }}
            </p>
            <p>
              <IconStorage style="color: #52c41a; margin-right: 4px" />
              <strong>内存：</strong>
              <a-tag color="green">
                {{ parseJudgeInfo(record.judgeInfo).memory }} KB
              </a-tag>
            </p>
            <p>
              <IconClockCircle style="color: #fa8c16; margin-right: 4px" />
              <strong>时间：</strong>
              <a-tag color="blue">
                {{ parseJudgeInfo(record.judgeInfo).time }} ms
              </a-tag>
            </p>
          </a-card>
        </div>
        <span v-else style="color: gray">无判题信息</span>
      </template>

      <!-- 判题状态 -->
      <template #status="{ record }">
        <span v-if="record.status === 2" style="color: green">
          ✅ 运行成功
        </span>
        <span v-else-if="record.status === 3" style="color: red">
          ❌ 编译失败 / 运行错误
        </span>
        <span v-else style="color: gray">未知状态</span>
      </template>

      <!-- 创建时间 -->
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionSubmitQueryRequest,
} from "../../../generated";
import { Message } from "@arco-design/web-vue";
import { useRouter } from "vue-router";
import moment from "moment";

// ✅ 使用 Arco 内置图标，不需要额外安装包
import {
  IconInfoCircle,
  IconClockCircle,
  IconStorage,
} from "@arco-design/web-vue/es/icon";

const router = useRouter();
const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionSubmitQueryRequest>({
  questionId: undefined,
  language: undefined,
  pageSize: 10,
  current: 1,
});

// 加载数据
const loadData = async () => {
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    {
      ...searchParams.value,
      sortField: "createTime",
      sortOrder: "descend",
    }
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    Message.error("加载失败" + res.message);
  }
};

// 解析判题信息
const parseJudgeInfo = (info: any) => {
  try {
    if (typeof info === "string") {
      return JSON.parse(info);
    }
    return info || {};
  } catch (e) {
    return {};
  }
};

onMounted(() => {
  loadData();
});

const columns = [
  { title: "提交号", dataIndex: "id" },
  { title: "编程语言", dataIndex: "language" },
  { title: "判题信息", slotName: "judgeInfo" },
  { title: "判题状态", dataIndex: "status", slotName: "status" },
  { title: "题目 id", dataIndex: "questionId", slotName: "questionId" },
  { title: "提交者 id", dataIndex: "userId", slotName: "userId" },
  { title: "创建时间", slotName: "createTime" },
];

watchEffect(() => {
  loadData();
});

// 分页切换
const onPageChange = (page: number) => {
  searchParams.value = { ...searchParams.value, current: page };
};

// 跳转到做题页面
const toQuestionPage = (question: Question) => {
  router.push({ path: `/view/question/${question.id}` });
};

// 搜索
const doSubmit = () => {
  searchParams.value = { ...searchParams.value, current: 1 };
};
</script>

<style scoped>
#questionSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
